<?php

if(!defined("INDEX"))
    die;

if(!check_post('artistname', 'old_artistname', 'year_formed', 'year_disbanded',
    'country', 'homepage', 'myspace'))
    throw new HackException();

// ----------------------------------------------------------------------------
    
isset($_GET['update']) ? $update = true : $update = false;

$smarty->assign('update', $update);

$dbo = DBO::getInstance();

$dbo->autoCommit(false);

try {

    $artist = new Artist;

    if(isset($_POST['artist_id'])) {
        $artist->id = $_POST['artist_id'];
        $artist->load();
    }

    $artist->name = $_POST['artistname'];
    $artist->old_name = $_POST['old_artistname'];
    $artist->year_formed = $_POST['year_formed'];
    $artist->year_disbanded = $_POST['year_disbanded'];
    $artist->country = $_POST['country'];
    $artist->homepage = $_POST['homepage'];
    $artist->myspace = $_POST['myspace'];

    if(isset($_POST['unsigned']))
        $artist->signed = !empty($_POST['unsigned']);
    else
        $unsigned = NULL;

    if(!empty($_FILES['picture']['name']))
        $artist->has_picture = true;
    elseif(!empty($_POST['picture_url']))
        $artist->has_picture = true;
    elseif(!empty($_POST['has_picture']))
        $artist->has_picture = true;

    $id = $artist->save();

    $_label_year = null;
    $_release_year = null;
    $_member_year_started = null;
    $_member_year_finished = null;

    foreach($_POST as $key => $value) {

        /* TAGS */
        if(strpos($key, 'tag') !== false) {
            if($update) {
                if(!$artist->linkedWithTag($value))
                    $artist->linkWithTag($value);
            } else
                $artist->linkWithTag($value);
        }

        /* LABELS */
        elseif(strpos($key, 'label_year') !== false) {
            $_label_year = $value;
        }

        elseif(strpos($key, 'label') !== false) {

            if($update) {
                if(!$artist->linkedWithLabel($value, $_label_year))
                    $artist->linkWithLabel($value, $_label_year);
            } else
                $artist->linkWithLabel($value, $_label_year);

            $_label_year = null;
        }

        /* RELEASES */
        elseif(strpos($key, 'release_year') !== false) {
            $_release_year = $value;
        }
        
        elseif(strpos($key, 'release') !== false) {

            if($update) {
                if(!$artist->linkedWithRelease($value, $_release_year)) {

                    $release = new Release;

                    $release->year_finished = $_release_year;
                    $release->name = $value;

                    $release_id = $release->save();

                    $artist->linkWithRelease($release_id);
                }
            } else {
                    $release = new Release;

                    $release->year_finished = $_release_year;
                    $release->name = $value;

                    $release_id = $release->save();

                    $artist->linkWithRelease($release_id);
            }

            $_release_year = null;
        }

        elseif(strpos($key, 'member_year_started') !== false) {
            $_member_year_started = $value;
        }

        elseif(strpos($key, 'member_year_finished') !== false) {
            $_member_year_finished = $value;
        }

        elseif(strpos($key, 'member') !== false) {

            if($update) {
                if(!$artist->linkedWithMember($value, $_member_year_started, $_member_year_finished))
                    $artist->linkWithMember($value, $_member_year_started, $_member_year_finished);
            } else
                $artist->linkWithMember($value, $_member_year_started, $_member_year_finished);

            $_member_year_finished = null;
            $_member_year_started = null;
        }

    }

    // Upload picture
    if(!empty($_FILES['picture']['name'])) {

        $artist->uploadPicture($_FILES['picture']);

    } elseif(!empty($_POST['picture_url'])) {

        $artist->uploadPictureFromUrl($_POST['picture_url']);
    }

    // Linking with lastfm tags
    try {
        $lastfm = new Lastfm;
        $lastfm->updateArtistTags($artist->id);
        $lastfm->updateArtistPicture($artist->id);
    } catch (Exception $e) {
        // TODO: log it
    }

    $dbo->commit();

    $smarty->assign('artist_name', $artist->name);

} catch(CoreException $e) {

    $dbo->rollback();

    $smarty->assign('errors', array(_('Core exception occured. Sorry!')));

} catch(Exception $e) {

    $dbo->rollback();

    $smarty->assign('errors', array($e->getMessage()));
}